from django.contrib import admin
from .models import Comment, CommentReply, CommentReport


class CommentReplyInline(admin.TabularInline):
    model = CommentReply
    extra = 0
    readonly_fields = ['user', 'content', 'created_at']
    can_delete = False


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ['book', 'user', 'rating', 'is_approved', 'is_verified', 'created_at']
    list_filter = ['is_approved', 'is_verified', 'rating', 'created_at']
    search_fields = ['book__title', 'user__email', 'title', 'content']
    readonly_fields = ['user', 'book', 'created_at']
    inlines = [CommentReplyInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('book', 'user', 'rating', 'title')
        }),
        ('内容', {
            'fields': ('content',)
        }),
        ('状态', {
            'fields': ('is_approved', 'is_verified', 'approved_by', 'approved_at')
        }),
        ('统计', {
            'fields': ('helpful_count', 'unhelpful_count'),
            'classes': ('collapse',)
        }),
        ('时间', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )


@admin.register(CommentReply)
class CommentReplyAdmin(admin.ModelAdmin):
    list_display = ['comment', 'user', 'created_at']
    search_fields = ['comment__book__title', 'user__email', 'content']
    readonly_fields = ['comment', 'user', 'created_at']


@admin.register(CommentReport)
class CommentReportAdmin(admin.ModelAdmin):
    list_display = ['comment', 'reporter', 'reason', 'status', 'created_at']
    list_filter = ['status', 'reason', 'created_at']
    search_fields = ['comment__book__title', 'reporter__email', 'description']
    readonly_fields = ['comment', 'reporter', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('comment', 'reporter', 'reason')
        }),
        ('描述', {
            'fields': ('description',)
        }),
        ('处理', {
            'fields': ('status', 'processed_by', 'processed_at', 'admin_note')
        }),
        ('时间', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        })
    )
